package Tools
{
    import flash.display.*;
    import flash.events.*;
	import flash.ui.Keyboard;
	import flash.utils.*;
	import Sync.*;
	import logic.GameLogic;
	import entity.*;
 
    public class Input extends Object
    {
        public var last:FocusEvent;
        public var mouseButtonHit:int = 0;
        public var focusStatus:int = 0;
        public var keyStatus:Array;
        public var mousex:int;
        public var mousey:int;
        public var mouseStatus:int = 0;
        public var keysHit:Array;
        public static var verboseInput:Boolean = false;
        public static var s_input:Input = null;

        public function Input()
        {
        }
		
		public static function getInstance():Input
		{
			if (s_input == null)
			{
				s_input = new Input();
			}
			
			return s_input;
		}
		
		public function initialize(param1:DisplayObject):void
		{
			keyStatus = new Array();
			keysHit = new Array();
			param1.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
			param1.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
			param1.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			param1.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			param1.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			param1.addEventListener(FocusEvent.FOCUS_IN, focusInHandler);
			param1.addEventListener(FocusEvent.FOCUS_OUT, focusOutHandler);
			param1.addEventListener(FocusEvent.FOCUS_OUT, focusOutHandler2);
			param1.addEventListener(FocusEvent.KEY_FOCUS_CHANGE, focusOutHandler);
			param1.addEventListener(FocusEvent.MOUSE_FOCUS_CHANGE, focusOutHandler);
		}
		
		public function release():void
		{
			
		}

        public function mouseY() : int
        {
            return mousey;
        }

        public function mouseUpHandler(event:MouseEvent) : void
        {
            mouseStatus = 0;
        }

        public function outOfFocus() : Boolean
        {
            return !focusStatus;
        }

        public function mouseMoveHandler(event:MouseEvent) : void
        {
            mousex = event.stageX;
            mousey = event.stageY;
        }

        public function mouseX() : int
        {
            return mousex;
        }

        public function focusOutHandler(event:FocusEvent) : void
        {
            var i:int = 0;
            if (last)
            {
                if (event.type == FocusEvent.FOCUS_OUT)
                {
                    if (last.type != FocusEvent.MOUSE_FOCUS_CHANGE)
                    {
                        i = 0;
                        while (i < keyStatus.length)
                        {
                            
                            keyStatus[i] = false;
                            i++;
                        }
                        mouseStatus = 0;
                    }
                }
            }
            last = event;
        }

        public function focusOutHandler2(event:FocusEvent) : void
        {
            focusStatus = 0;
        }

        public function keyDown(param1:int) : int
        {
            return keyStatus[param1];
        }

        public function mouseDownHandler(event:MouseEvent) : void
        {
			var selfID:int = GameLogic.getInstance().getSelfPlayerHandleID();
			var entityPlayer:EntityPlayer = EntityManager.getInstance().getEntity(selfID) as EntityPlayer;
			if (entityPlayer.getLifeNum() == 0)
				return;
			
            mouseStatus = 1;
            mouseButtonHit = 1;
			
			
			
			var stream:Stream = SyncSystem.getInstance().beginInstruct(selfID, selfID, ConstValue.INST_FIRE);
			stream.writeInt(event.stageX);
			stream.writeInt(event.stageY);
			SyncSystem.getInstance().endInstruct();
        }

        public function mouseDown() : int
        {
            return mouseStatus;
        }

        public function keyDownHandler(event:KeyboardEvent) : void
        {
            if (keyStatus[event.keyCode] != true)
            {
                keysHit[event.keyCode] = true;
				
				if (event.keyCode == Keyboard.W ||
					event.keyCode == Keyboard.S ||
					event.keyCode == Keyboard.A ||
					event.keyCode == Keyboard.D)
				{
					var selfID:int = GameLogic.getInstance().getSelfPlayerHandleID();
					
					var entityPlayer:EntityPlayer = EntityManager.getInstance().getEntity(selfID) as EntityPlayer;
					if (entityPlayer.getLifeNum() == 0)
						return;					
					
					
					var stream:Stream = SyncSystem.getInstance().beginInstruct(selfID, selfID, ConstValue.INST_KEY_DOWN);
					stream.writeInt(event.keyCode);
					SyncSystem.getInstance().endInstruct();
					
					trace("key down send " + getTimer());
				}
            }
            keyStatus[event.keyCode] = true;
            if (verboseInput)
            {
                trace(event.keyCode);
            }
            return;
        }
		
		public function keyUpHandler(event:KeyboardEvent) : void
		{
			if (event.keyCode == Keyboard.W ||
				event.keyCode == Keyboard.S ||
				event.keyCode == Keyboard.A ||
				event.keyCode == Keyboard.D)
			{
				var selfID:int = GameLogic.getInstance().getSelfPlayerHandleID();
				
				var entityPlayer:EntityPlayer = EntityManager.getInstance().getEntity(selfID) as EntityPlayer;
				if (entityPlayer.getLifeNum() == 0)
					return;		
				
				var stream:Stream = SyncSystem.getInstance().beginInstruct(selfID, selfID, ConstValue.INST_KEY_UP);
				stream.writeInt(event.keyCode);
				SyncSystem.getInstance().endInstruct();
			}
			
			keyStatus[event.keyCode] = false;
		}


        public function focusInHandler(event:FocusEvent) : void
        {
            focusStatus = 1;
            return;
        }

        public function mouseHit() : int
        {
            var _loc_1:* = mouseButtonHit;
            mouseButtonHit = 0;
            return _loc_1;
        }

        public function keyHit(param1:int, param2:Boolean = true) : int
        {
            var _loc_3:* = keysHit[param1];
            if (param2)
            {
                keysHit[param1] = false;
            }
            return _loc_3;
        }

    }
}
